﻿#region Copyright (c) 2009, Helori LAMBERTY
/*
[===========================================================================]
[   Copyright (c) 2009, Helori LAMBERTY                                     ]
[   All rights reserved.                                                    ]
[                                                                           ]
[   Redistribution and use in source and binary forms, with or without      ] 
[   modification, are permitted provided that the following conditions      ]
[   are met:                                                                ]
[                                                                           ]  
[   * Redistributions of source code must retain the above copyright        ]
[   notice, this list of conditions and the following disclaimer.           ]
[                                                                           ]
[   * Redistributions in binary form must reproduce the above copyright     ]
[   notice, this list of conditions and the following disclaimer in         ]
[   the documentation and/or other materials provided with the              ]
[   distribution.                                                           ]
[                                                                           ]
[   * Neither the name of NotesFor.net nor the names of its                 ]
[   contributors may be used to endorse or promote products derived         ]
[   from this software without specific prior written permission.           ]   
[                                                                           ]
[   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS     ]
[   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT       ]
[   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR   ]
[   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT    ]
[   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   ]
[   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT        ]
[   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,   ]
[   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY   ]
[   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT     ]
[   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE       ] 
[   USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH        ] 
[   DAMAGE.                                                                 ]
[===========================================================================]
*/
#endregion
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.ComponentModel;
using System.Security.Permissions;
using System.Drawing.Design;

namespace NotesFor
{
    /// <summary>
    /// Summary description for Gallery
    /// </summary>
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [ToolboxData("<{0}:Gallery runat=\"server\"></{0}:Gallery>")]
    [ToolboxBitmap(typeof(Gallery), "NFGallery.bmp")]
    public class Gallery : ScriptControl
    {
        #region Properties

        private string folderUrl;
        /// <summary>
        /// The URL folder containing the images. 
        /// </summary>
        [Category("Behavior")]
        [Description("The URL folder containing the images. ")]
        public string FolderUrl
        {
            get { return folderUrl; }
            set { folderUrl = value; }
        }
        private int size;
        /// <summary>
        /// Max size height/width for the image thumbnail. 
        /// </summary>
        [Category("Layout")]
        [DefaultValue(100)]
        [Description("Max size (height/width) for the image thumbnail. ")]
        public int Size
        {
            get { return size; }
            set { size = value; }
        }
        private string cssClass;
        /// <summary>
        /// CSS Class name applied to the control. 
        /// </summary>
        [Category("Appearance")]
        [Description("CSS Class name applied to the control. ")]
        public override string CssClass
        {
            get { return cssClass; }
            set { cssClass = value; }
        } 

        #endregion

        public Gallery()
        {
        }

        #region OnInit event

        protected override void OnInit(EventArgs e)
        {
            RegisterCss("NotesFor.css.jquery.lightbox-0.5.css");
            RegisterCss("NotesFor.css.gallery.css");

            RegisterJavascript("NotesFor.js.jquery-1.3.2.min.js");
            RegisterJavascript("NotesFor.js.jquery.lightbox-0.5.min.js");
        }

        private void RegisterCss(string css)
        {
            string cssUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), css);
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = cssUrl;
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            this.Page.Header.Controls.Add(cssLink);
        }

        private void RegisterJavascript(string js)
        {
            string jsUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), js);
            HtmlGenericControl jsTag = new HtmlGenericControl("script");
            jsTag.Attributes.Add("type", "text/javascript");
            jsTag.Attributes.Add("src", jsUrl);
            this.Page.Header.Controls.Add(jsTag);
        } 

        #endregion

        protected override IEnumerable<ScriptDescriptor>
                GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("NotesFor.Gallery", this.ClientID);
            string path = HttpContext.Current.Server.MapPath(FolderUrl);
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
            List<string> fileNames = new List<string>();
            if (di.Exists)
            {
                foreach (System.IO.FileInfo fi in di.GetFiles())
                {
                    string extension = fi.Extension.ToLower();
                    if (extension == ".jpg" || extension == ".gif" || extension == ".png")
                        fileNames.Add(fi.Name);
                }
            }

            if (FolderUrl[FolderUrl.Length - 1] != '/' && FolderUrl[FolderUrl.Length - 1] != '\\')
                FolderUrl = FolderUrl + "/";

            descriptor.AddProperty("handlerUrl", ResolveUrl("~/ThumbnailHandler.ashx"));
            descriptor.AddProperty("folderUrl", ResolveUrl(FolderUrl));
            if (!string.IsNullOrEmpty(CssClass))
                descriptor.AddProperty("cssClass", CssClass);
            descriptor.AddProperty("files", fileNames.ToArray());
            descriptor.AddProperty("size", Size);
            descriptor.AddProperty("totalFiles", fileNames.Count);
            yield return descriptor;
        }

        // Generate the script reference
        protected override IEnumerable<ScriptReference>
                GetScriptReferences()
        {
            yield return new ScriptReference("NotesFor.Gallery.js", this.GetType().Assembly.FullName);
        }
    }
}